package eu.jpereira.trainings.designpatterns.structural.adapter.thirdparty;

import eu.jpereira.trainings.designpatterns.structural.adapter.exceptions.CodeMismatchException;
import eu.jpereira.trainings.designpatterns.structural.adapter.exceptions.IncorrectDoorCodeException;
import eu.jpereira.trainings.designpatterns.structural.adapter.model.Door;

public class ThirdPartyDoorObjectAdapter implements Door {

	private String code = ThirdPartyDoor.DEFAULT_CODE;

	private boolean open = false;
	
	public ThirdPartyDoorObjectAdapter() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void open(String code) throws IncorrectDoorCodeException {
		// TODO Auto-generated method stub
		// try the code
		if (this.code.equals(code)) {
			this.open = true;
		} else {
			throw new IncorrectDoorCodeException();
		}

	}

	@Override
	public void close() {
		// TODO Auto-generated method stub
		this.open = false;

	}

	@Override
	public boolean isOpen() {
		// TODO Auto-generated method stub
		return this.open;
		//return false;
	}

	@Override
	public void changeCode(String oldCode, String newCode,
			String newCodeConfirmation) throws IncorrectDoorCodeException,
			CodeMismatchException {
		// TODO Auto-generated method stub
		if (newCode.equals(newCodeConfirmation)) {
			if (oldCode.equals(this.code)) {
				this.code = newCode;
			} else {
				throw new IncorrectDoorCodeException();
			}
		} else {
			throw new CodeMismatchException();
		}

	}

	@Override
	public boolean testCode(String code) {
		// TODO Auto-generated method stub
		return code.equals(this.code);
		//return false;
	}

}
